/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.web.actionbeans;

import cz.muni.fi.pa165.ddtroops.dto.UserDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.UserService;
import javax.persistence.PersistenceException;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;

/**
 *
 * @author Erik
 */
@UrlBinding("/adminregistration/")
public class AdminRegistrationActionBean implements ActionBean {
    
    private ActionBeanContext context;
    @SpringBean(value="userWebService")
    private UserService service;
    
        @ValidateNestedProperties({
        @Validate(field="name", required=true, minlength=4, maxlength=16),
        @Validate(field="password", required=true, minlength=4, maxlength=16)
    })
    
    private UserDTO user;
       
        @DefaultHandler
        public Resolution registerAdmin(){
        user.setAuthority("ROLE_SUPERVISOR");
        PasswordEncoder encoder = new ShaPasswordEncoder();
        String hashedPass = encoder.encodePassword(user.getPassword(), user.getName());
        user.setPassword(hashedPass);
        
        try{
        service.create(user);}
        catch(PersistenceException ex){
            getContext().getMessages().add(new LocalizableError("usernameTaken", user));
            return new RedirectResolution("/registration.jsp?reg_error=1");
        }
        
        getContext().getMessages().add(new LocalizableError(
        getClass().getName() + ".successMessage", this.user.getName(), this.user.getPassword()
            ));
    return new RedirectResolution("/login.jsp");
    }
    
    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public UserService getService() {
        return service;
    }

    public void setService(UserService service) {
        this.service = service;
    }

    public UserDTO getUser() {
        return user;
    }

    public void setUser(UserDTO user) {
        this.user = user;
    }

    
    @Validate(required=true, minlength=4, maxlength=16, expression="confirmPassword == user.password")
    
    private String confirmPassword;

    @ValidationMethod
    public void validate(ValidationErrors errors){
        if (service.getAll().contains(user)){
            errors.add("user.username", new LocalizableError("usernameTaken"));
        }
    }
    
    
    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
}
